﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpiderNet8.Main
{
    public class GetContext
    {
        private readonly ILogger<GetContext> _logger;
        private readonly IConfiguration _configuration;
        private readonly HttpClient _httpClient;

        public GetContext(ILogger<GetContext> logger, IConfiguration configuration, HttpClient httpClient)
        {
            _logger = logger;
            _configuration = configuration;
            _httpClient = httpClient;
        }

        // 从URL获取HTML内容
        public async Task<string> GetContentByHtmlAsync(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    _logger.LogError("URL不能为空");
                    return string.Empty;
                }

                var response = await _httpClient.GetAsync(url);
                response.EnsureSuccessStatusCode();

                return await response.Content.ReadAsStringAsync();
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError(ex, $"获取URL内容失败: {url}");
                return string.Empty;
            }
        }

        // 从URL获取JSON内容
        public async Task<string> GetHtmlByJsonAsync(string url)
        {
            // 这里方法名可能有误，推测应该是从JSON API获取内容
            return await GetContentByHtmlAsync(url);
        }
    }
}
